package com.sora.org.service.impl;

import com.sora.basic.exception.BusinessException;
import com.sora.basic.service.impl.BaseServiceImpl;
import com.sora.basic.util.BaiduAiUtils;
import com.sora.basic.util.Md5Utils;
import com.sora.basic.util.StrUtils;
import com.sora.org.domain.Employee;
import com.sora.org.domain.Shop;
import com.sora.org.domain.ShopAuditLog;
import com.sora.org.dto.ShopDto;
import com.sora.org.mapper.EmployeeMapper;
import com.sora.org.mapper.ShopAuditLogMapper;
import com.sora.org.mapper.ShopMapper;
import com.sora.org.service.IShopService;
import com.sora.user.domain.Logininfo;
import com.sora.user.mapper.LogininfoMapper;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.mail.SimpleMailMessage;
import org.springframework.mail.javamail.JavaMailSender;
import org.springframework.mail.javamail.MimeMessageHelper;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.mail.MessagingException;
import javax.mail.internet.MimeMessage;
import java.io.File;
import java.util.List;

/**
 * 业务实现类：
 */
@Service
public class ShopServiceImpl extends BaseServiceImpl<Shop> implements IShopService {

    @Autowired
    private ShopMapper shopMapper;
    @Autowired
    private EmployeeMapper employeeMapper;
    @Autowired
    private ShopAuditLogMapper shopAuditLogMapper;
    @Autowired
    private JavaMailSender javaMailSender;
    @Autowired
    private LogininfoMapper logininfoMapper;

    @Override
    @Transactional
    public void settlement(Shop shop) {
        //一、校验
        //1.空值校验
        if(StringUtils.isEmpty(shop.getName()) || StringUtils.isEmpty(shop.getAddress())){
            throw new BusinessException("信息不能为空！！！");
        }
        //2.两次密码不一致校验
        if(!shop.getAdmin().getPassword().equals(shop.getAdmin().getComfirmPassword())){
            throw new BusinessException("两次密码不一致！！！");
        }
        //3.当前店铺是否入驻过
        Shop dbShop = shopMapper.findByNameAndAddress(shop);
        if(dbShop != null){
            throw new BusinessException("当前店铺已经入驻过，请直接登录！！！");
        }

        //二、百度AI智能审核【店铺名，logo是否合法】
        //判断店铺名是否合规
        if(!BaiduAiUtils.TextCensor(shop.getName())){
            throw new BusinessException("当前店铺名不合规！！！");
        }
        //判断店铺logo是否合规
        if(!BaiduAiUtils.ImgCensor("http://123.207.27.208"+shop.getLogo())){
            throw new BusinessException("当前店铺logo不合规！！！");
        }

        //三、添加到数据库
        //1.先添加员工信息
        Employee admin = shop.getAdmin();

        //处理盐值和密码
        String salt = StrUtils.getComplexRandomString(32);
        String md5Pwd = Md5Utils.encrypByMd5(admin.getPassword() + salt);
        admin.setSalt(salt);
        admin.setPassword(md5Pwd);

        //添加logininfo信息
        Logininfo logininfo = new Logininfo();
        BeanUtils.copyProperties(admin,logininfo);
        logininfo.setType(0);//管理员
        logininfo.setDisable(true);//可以登陆
        logininfoMapper.save(logininfo);

        //将自增长的logininfo_id设置到admin对象中
        admin.setLogininfo_id(logininfo.getId());

        employeeMapper.save(admin);
        //2.再添加店铺信息
        shop.setAdminId(admin.getId());
        shopMapper.save(shop);
        //3.将店铺id更新到员工表中
        admin.setShop_id(shop.getId());
        employeeMapper.update(admin);

        //四、人工审核之后发送激活邮件
    }

    @Override
    public void auditPass(ShopAuditLog log) throws MessagingException {
        //1.改状态：1待审核 - 2审核成功待激活
        shopMapper.updateState(2,log.getShopId());
        //2.添加审核日志
        shopAuditLogMapper.save(log);
        //3.发邮件给店长通知他重新修改信息入驻
        //创建复杂邮件对象
        MimeMessage mimeMessage = javaMailSender.createMimeMessage();
        //发送复杂邮件的工具类
        //true - 能够添加附件
        MimeMessageHelper helper = new MimeMessageHelper(mimeMessage,true,"utf-8");
        helper.setFrom("1203269902@qq.com");
        helper.setSubject("店铺激活邮件");
        Shop shop = shopMapper.loadById(log.getShopId());
        //true - 能够再内容中编写html标签 - 会解析
        helper.setText("<h1>你的店铺已经注册!!!</h1><img src='http://123.207.27.208"+shop.getLogo()+"'><a href='http://172.16.6.204:8080/shop/active/"+shop.getId()+"'>点击该链接激活</a>",true);
        //添加附件
        helper.addAttachment("p24.jpg",new File("D:\\javastudy\\课堂代码\\03_images\\项目二宠物图片\\p24.jpg"));

        Employee employee = employeeMapper.loadById(shop.getAdminId());
        //收件人
        helper.setTo(employee.getEmail());
        javaMailSender.send(mimeMessage);
    }

    @Override
    public void auditReject(ShopAuditLog log) {
        //1.改状态：1待审核 - 4审核失败驳回
        shopMapper.updateState(4,log.getShopId());
        //2.添加审核日志
        log.setState(0);//审核失败
        log.setAuditId(350L); //登录没做之前先写死
        shopAuditLogMapper.save(log);
        //3.发邮件给店长通知他重新修改信息入驻
        SimpleMailMessage mailMessage = new SimpleMailMessage();
        //设置发送人
        mailMessage.setFrom("1203269902@qq.com");
        //邮件主题
        mailMessage.setSubject("店铺入驻被驳回");
        //邮件内容：普通文件无法解析html标签
        mailMessage.setText("<h1>店铺入驻被驳回，请重新入驻！！！</h1>");

        Shop shop = shopMapper.loadById(log.getShopId());
        Employee employee = employeeMapper.loadById(shop.getAdminId());
        //收件人
        mailMessage.setTo(employee.getEmail());
        //发送邮件
        javaMailSender.send(mailMessage);
    }

    @Override
    public void activeShop(Long id) {
        shopMapper.updateState(3,id);
    }

    @Override
    public List<ShopDto> echarts() {
        return shopMapper.echarts();
    }

}
